/**
 * 高德地图微信小程序SDK
 * 官方文档: https://lbs.amap.com/api/wx/guide/get-data/regeo
 */
function AMapWX(a) {
    this.key = a.key;
    this.requestConfig = {
        key: a.key,
        s: "rsx",
        platform: "WXJS",
        appname: a.key,
        sdkversion: "1.2.0",
        logversion: "2.0"
    };
}

AMapWX.prototype.getWxLocation = function(a, b) {
    wx.getLocation({
        type: "gcj02",
        success: function(c) {
            c = c.longitude + "," + c.latitude;
            wx.setStorage({
                key: "userLocation",
                data: c
            });
            b(c);
        },
        fail: function(c) {
            wx.getStorage({
                key: "userLocation",
                success: function(d) {
                    d.data && b(d.data);
                }
            });
            a.fail({
                errCode: "0",
                errMsg: c.errMsg || ""
            });
        }
    });
};

AMapWX.prototype.getRegeo = function(a) {
    function b(c) {
        var d = e.requestConfig;
        wx.request({
            url: "https://restapi.amap.com/v3/geocode/regeo",
            data: {
                key: e.key,
                location: c,
                extensions: "all",
                s: d.s,
                platform: d.platform,
                appname: e.key,
                sdkversion: d.sdkversion,
                logversion: d.logversion
            },
            method: "GET",
            header: {
                "content-type": "application/json"
            },
            success: function(g) {
                var f = g.data;
                if (f.status && "1" == f.status) {
                    f = f.regeocode;
                    var h = f.addressComponent,
                        i = [],
                        j = f.roads[0].name + "附近",
                        k = c.split(",")[0],
                        l = c.split(",")[1];
                    if (f.pois && f.pois[0]) {
                        j = f.pois[0].name + "附近";
                        var m = f.pois[0].location;
                        m && (k = parseFloat(m.split(",")[0]), l = parseFloat(m.split(",")[1]));
                    }
                    h.provinceName && i.push(h.provinceName);
                    h.cityName && i.push(h.cityName);
                    h.districtName && i.push(h.districtName);
                    h.streetName && i.push(h.streetName);
                    i = i.join("");
                    var regeocodeData = {
                        formatted_address: i,
                        addressComponent: {
                            province: h.provinceName || '',
                            city: h.cityName || '',
                            district: h.districtName || '',
                            street: h.streetName || ''
                        }
                    };
                    a.success([{
                        id: 0,
                        title: j,
                        latitude: l,
                        longitude: k,
                        address: i,
                        distance: 0,
                        name: j,
                        regeocodeData: regeocodeData
                    }]);
                } else a.fail({
                    errCode: f.infocode,
                    errMsg: f.info
                });
            },
            fail: function(g) {
                a.fail({
                    errCode: "0",
                    errMsg: g.errMsg || ""
                });
            }
        });
    }
    var e = this;
    a.location ? b(a.location) : e.getWxLocation(a, function(c) {
        b(c);
    });
};

AMapWX.prototype.getInputtips = function(a) {
    var b = this.requestConfig;
    a.location = a.location || "";
    a.keywords = a.keywords || "";
    wx.request({
        url: "https://restapi.amap.com/v3/assistant/inputtips",
        data: {
            key: this.key,
            s: b.s,
            platform: b.platform,
            appname: this.key,
            sdkversion: b.sdkversion,
            logversion: b.logversion,
            keywords: a.keywords,
            location: a.location
        },
        method: "GET",
        header: {
            "content-type": "application/json"
        },
        success: function(c) {
            c = c.data;
            c.tips && a.success({
                tips: c.tips
            });
        },
        fail: function(c) {
            a.fail({
                errCode: "0",
                errMsg: c.errMsg || ""
            });
        }
    });
};

AMapWX.prototype.getPoiAround = function(a) {
    function b(c) {
        var d = e.requestConfig;
        a.querytypes = a.querytypes || "";
        a.querykeywords = a.querykeywords || "";
        wx.request({
            url: "https://restapi.amap.com/v3/place/around",
            data: {
                key: e.key,
                location: c,
                keywords: a.querykeywords,
                types: a.querytypes,
                radius: a.radius || 1000,
                offset: a.offset || 20,
                page: a.page || 1,
                extensions: "all",
                s: d.s,
                platform: d.platform,
                appname: e.key,
                sdkversion: d.sdkversion,
                logversion: d.logversion
            },
            method: "GET",
            header: {
                "content-type": "application/json"
            },
            success: function(g) {
                if (g.data.status && "1" == g.data.status) {
                    if (g.data && g.data.pois) {
                        for (var f = [], h = 0; h < g.data.pois.length; h++) {
                            var i = 0 == h ? a.iconPathSelected : a.iconPath;
                            if (a.showMarker && !1 !== a.showMarker) {
                                var j = {
                                    iconPath: i || "./marker.png",
                                    width: 22,
                                    height: 32,
                                    name: g.data.pois[h].name,
                                    desc: g.data.pois[h].address,
                                    longitude: parseFloat(g.data.pois[h].location.split(",")[0]),
                                    latitude: parseFloat(g.data.pois[h].location.split(",")[1]),
                                    id: h
                                };
                                f.push(j);
                            }
                        }
                        a.success({
                            markers: f,
                            poisData: g.data.pois
                        });
                    }
                } else a.fail({
                    errCode: g.data.infocode,
                    errMsg: g.data.info
                });
            },
            fail: function(g) {
                a.fail({
                    errCode: "0",
                    errMsg: g.errMsg || ""
                });
            }
        });
    }
    var e = this;
    a.location ? b(a.location) : e.getWxLocation(a, function(c) {
        b(c);
    });
};

module.exports.AMapWX = AMapWX;